package cn.ztuo.bitrade.consumer;

import cn.ztuo.bitrade.constant.TransactionType;
import cn.ztuo.bitrade.dao.MemberTransactionDao;
import cn.ztuo.bitrade.entity.EarningsBuyRecord;
import cn.ztuo.bitrade.entity.EarningsTreasure;
import cn.ztuo.bitrade.entity.MemberTransaction;
import cn.ztuo.bitrade.entity.MemberWallet;
import cn.ztuo.bitrade.service.EarningsBuyRecordService;
import cn.ztuo.bitrade.service.EarningsTreasureService;
import cn.ztuo.bitrade.service.MemberWalletService;
import cn.ztuo.bitrade.util.BigDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: Xiazt
 * @date: 2020-04-09 15:08
 */
@Slf4j
@Component
public class EarningsBuyRecordConsumer {

    @Autowired
    private EarningsBuyRecordService earningsBuyRecordService;

    @Autowired
    private EarningsTreasureService earningsTreasureService;

    @Autowired
    private MemberWalletService memberWalletService;

    @Autowired
    private MemberTransactionDao memberTransactionDao;

    @KafkaListener(topics = "earningsBuyRecord")
    @Transactional
    public void earningsBuyRecord(List<ConsumerRecord<String, String>> records) {
        for (int i = 0; i < records.size(); i++) {
            ConsumerRecord<String, String> record = records.get(i);
            log.info("topic={},accessKey={},value={}", record.topic(), record.key(), record.value());

            EarningsBuyRecord earningsBuyRecord = earningsBuyRecordService.getById(Long.parseLong(record.key()));
            // 修改状态为已获得收益
            earningsBuyRecord.setStatus(1);
            earningsBuyRecordService.inster(earningsBuyRecord);

            // 修改账户余额 = 余额+本金+收益
            EarningsTreasure earningsTreasure = earningsTreasureService.getById(earningsBuyRecord.getEarnings().getId());
            MemberWallet memberWallet = memberWalletService.findOneByCoinNameAndMemberId(earningsTreasure.getCoin().getName(), earningsBuyRecord.getMember().getId());
            BigDecimal principalAndEarnings = BigDecimalUtils.add(earningsBuyRecord.getBuyAmount(), earningsBuyRecord.getExpectedEarnings());
            memberWallet.setBalance(BigDecimalUtils.add(memberWallet.getBalance(), principalAndEarnings));
            memberWalletService.save(memberWallet);

            // 资产流水
            MemberTransaction memberTransaction = new MemberTransaction();
            memberTransaction.setMemberId(earningsBuyRecord.getMember().getId());
            memberTransaction.setSymbol(earningsTreasure.getCoin().getUnit());
            memberTransaction.setCreateTime(new Date());
            memberTransaction.setAmount(principalAndEarnings);
            memberTransaction.setType(TransactionType.EARNINGS_TREASURE_EXPIRE);
            memberTransactionDao.save(memberTransaction);
            log.info("memberId为{},收益宝收益结算完毕", earningsBuyRecord.getMember().getId());
        }
    }
}
